<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Cinema extends Admin
{
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::table('l_cinema')
                ->paginate(3,false,['query' => Request::instance()->param()]);

        return view('Cinema/index', [
            'list'=>$data
        ]);
    }


    public function add()
    {
        return view('Cinema/add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $rel = $request->post();
        $file = request()->file('image');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin1/img');
            if($info){
                $imgs = implode('/', explode('\\', $info->getSaveName()));
                $img = '/static/admin1/img/'.$imgs;
            }else{
                $this->error('图片上传错误');
            }
        }
        $data = [
            'cinema'=>$rel['cinema'],
            'cinemaimg'=>$img,
            'town'=>$rel['town'],
            'site'=>$rel['site'],
            'traffic'=>$rel['traffic'],
            'phone'=>$rel['phone']
        ];
        $list = Db::table('l_cinema')
                ->insert($data);

        if($list){
            $this->success('添加成功', 'admin/Cinema/create');
        }else{
            $this->error('添加失败');
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = Db::table('l_cinema')
            ->where('id', $id)
            ->find();

        return view('Cinema/exit', [
            'list'=>$data
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $rel = $request->post();

        $file = request()->file('image');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin1/img');
            if ($info) {
                unlink('.'.$rel['img']);
                $imgs = implode('/', explode('\\', $info->getSaveName()));
                $img = '/static/admin1/img/' . $imgs;
                $data = [
                    'cinema' => $rel['cinema'],
                    'cinemaimg' => $img,
                    'town' => $rel['town'],
                    'site' => $rel['site'],
                    'traffic' => $rel['traffic'],
                    'phone' => $rel['phone']
                ];
            } else {
                $this->error('图片上传错误');
            }
        }else{
            $data = [
                'cinema'=>$rel['cinema'],
                'town'=>$rel['town'],
                'site'=>$rel['site'],
                'traffic'=>$rel['traffic'],
                'phone'=>$rel['phone']
            ];
        }

        $list = Db::table('l_cinema')
                ->where('id', $id)
                ->update($data);

        if($list){
            $this->success('修改成功', 'admin/Cinema/create');
        }else {
            $this->error('修改失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('l_cinema')
                ->where('id', $id)
                ->delete();
        if($result){
            $data = Db::table('l_studio')
                    ->where('cinema_id', $id)
                    ->delete();
        }

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的影院删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的影院删除失败,请重试!';
        }
        return json($info);
    }
    

     //影院搜索
     public function select()
     {
     $post = Request()->post();
     
     // var_dump($post);die;
     $list = db('l_cinema')->where('cinema','like','%'.$post['k'].'%')->paginate(3,false,['query' => Request::instance()->param()]);
     $this->assign('list',$list);
     // var_dump($list);die;
        return view('Cinema/index');
  
     }


}
